package com.slipper.system.codeless.service;

import java.util.Collections;
import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.slipper.util.Tool;
import com.slipper.util.UUID;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;

import com.slipper.system.codeless.dao.DataFormAutofillFieldDao;
import com.slipper.system.codeless.bo.DataFormAutofillFieldBO;

import com.slipper.core.transaction.ReadTransactional;
import com.slipper.core.transaction.WriteTransactional;

@Service
@ReadTransactional
public class DataFormAutofillFieldService {
    @Resource
    private DataFormAutofillFieldDao dataFormAutofillFieldDao;
    
    public DataFormAutofillFieldBO queryBy(String id) {
        return dataFormAutofillFieldDao.queryBy(id);
    }
    
    public List<DataFormAutofillFieldBO> queryList(DataFormAutofillFieldBO bo) {
        return dataFormAutofillFieldDao.queryList(bo);
    }
    
    public PageInfo<DataFormAutofillFieldBO> queryPage(DataFormAutofillFieldBO bo) {
        PageHelper.startPage(bo.getPageNumber(), bo.getPageSize());
        return new PageInfo<>(dataFormAutofillFieldDao.queryList(bo));
    }

    @WriteTransactional
    public int save(String autofillId, List<DataFormAutofillFieldBO> list, String employeeId) {
        if (Tool.isBlank(autofillId)) {
            return 0;
        }

        int i = 0;
        for (DataFormAutofillFieldBO field : list) {
            field.setAutofillId(autofillId);
            field.setOrdinal(++i);
            if (Tool.isBlank(field.getId())) {
                field.setId(UUID.getUUID());
                field.setUpdateId(employeeId);
                field.setCreateId(employeeId);
                field.setUpdateTime(System.currentTimeMillis());
                field.setCreateTime(System.currentTimeMillis());
                dataFormAutofillFieldDao.insert(field);
            } else {
                field.setUpdateId(employeeId);
                field.setUpdateTime(System.currentTimeMillis());
                dataFormAutofillFieldDao.update(field);
            }
        }

        // 删除被删除的数据
        DataFormAutofillFieldBO qo = new DataFormAutofillFieldBO();
        qo.setAutofillId(autofillId);
        dataFormAutofillFieldDao.queryList(qo).stream()
                .filter(field -> list.stream().noneMatch(temp -> field.getId().equals(temp.getId())))
                .forEach(field -> dataFormAutofillFieldDao.delete(field.getId()));

        return 1;
    }

    @WriteTransactional
    public void saveList(List<DataFormAutofillFieldBO> list) {
        if (list == null) return;

        list.forEach(item -> {
            if (dataFormAutofillFieldDao.queryBy(item.getId()) != null) {
                item.setUpdateTime(System.currentTimeMillis());
                dataFormAutofillFieldDao.update(item);
            } else {
                item.setUpdateTime(System.currentTimeMillis());
                item.setCreateTime(System.currentTimeMillis());
                dataFormAutofillFieldDao.insert(item);
            }
        });
    }

    @WriteTransactional
    public void deleteByAutofillId(String autofillId) {
        if (Tool.isBlank(autofillId)) {
            return;
        }

        dataFormAutofillFieldDao.deleteByAutofillId(autofillId);
    }

    @WriteTransactional
    public void copyByAutofillId(String oldAutofillId, String newAutofillId, String employeeId) {
        if (Tool.isBlank(oldAutofillId) || Tool.isBlank(newAutofillId)) {
            return;
        }

        DataFormAutofillFieldBO qo = new DataFormAutofillFieldBO();
        qo.setAutofillId(oldAutofillId);
        dataFormAutofillFieldDao.queryList(qo).forEach(field -> {
            field.setId(UUID.getUUID());
            field.setAutofillId(newAutofillId);
            field.setUpdateId(employeeId);
            field.setCreateId(employeeId);
            field.setUpdateTime(System.currentTimeMillis());
            field.setCreateTime(System.currentTimeMillis());
            dataFormAutofillFieldDao.insert(field);
        });
    }

    public List<DataFormAutofillFieldBO> queryByAutofillId(String autofillId) {
        if (Tool.isBlank(autofillId)) {
            return Collections.emptyList();
        }

        DataFormAutofillFieldBO qo = new DataFormAutofillFieldBO();
        qo.setAutofillId(autofillId);
        return dataFormAutofillFieldDao.queryList(qo);
    }
}
